var JsonUtil = require('JsonUtil');

window.TextUtil = (function(){

    var _instance;

    function init(){

        var TextMgrObj = {

            _isLoadFile: false,
            _loadIndex:0,
            _textMap: new Map(),

            getValue: function(key){
                return this._textMap.get(key);
            },

            initData: function(){

                JsonUtil.loadFile("resources/Json/texts.json", true, function(res){
                    if(!res)
                    {
                        if(TextMgrObj._loadIndex <= 1)
                        {
                            setTimeout(function(){
                                TextMgrObj.initData();
                            },50);
                            ++TextMgrObj._loadIndex;
                        }
                    }
                    else{
                        TextMgrObj._textMap = new Map();
                        for (var i = 0; i < res.length; ++i)
                        {
                            if(UIHelper.isChinese())
                            {
                                TextMgrObj._textMap.set(res[i].TID, res[i].CN);
                            }
                            else
                            {
                                TextMgrObj._textMap.set(res[i].TID, res[i].EN);
                            }                        
                           // cc.log("TextMgrObj._textMap: " +   TextMgrObj._textMap.get(res[i].TID));
                        }
                        TextMgrObj._isLoadFile = true;
                       }
                   });
            },

        };

        TextMgrObj.initData();

        return TextMgrObj;
    }



    var _static = {

        getInstance: function(){
            if(this._instance === undefined){
                this._instance = new init();
            }
            else if(!this._instance._isLoadFile)
            {
                this._instance.initData();
            }
            return this._instance;
        }
    }
    return _static;

})();

module.exports = TextUtil;

